package com.sczmgk.syevaluation.utils;

import android.os.Looper;
import android.util.Log;

import com.sczmgk.syevaluation.entity.base.BaseEntity;
import com.sczmgk.syevaluation.entity.input.LoginInput;
import com.sczmgk.syevaluation.entity.output.AppVersion;
import com.sczmgk.syevaluation.entity.output.LoginStatus;
import com.sczmgk.syevaluation.entity.output.UserMetter;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import okio.Buffer;
import okio.BufferedSource;
import okio.ForwardingSource;
import okio.Okio;
import okio.Source;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.Headers;
import retrofit2.http.POST;
import retrofit2.http.Streaming;
import retrofit2.http.Url;

public class DownLoadApi {


    private static final String TAG = DownLoadApi.class.getSimpleName();
    private static HttpInterf mApi;

    public static HttpInterf getService() {
        if (mApi == null) {
            Retrofit.Builder builder =   new Retrofit.Builder()
                    .baseUrl("http://xxx/");
            OkHttpClient client = new OkHttpClient.Builder()
                    .addNetworkInterceptor(new Interceptor() {
                        @Override
                        public okhttp3.Response intercept(Chain chain) throws IOException {
                            okhttp3.Response orginalResponse = chain.proceed(chain.request());

                            return orginalResponse.newBuilder()
                                    .body(new ProgressResponseBody(orginalResponse.body(), new ProgressListener() {
                                        @Override
                                        public void onProgress(long progress, long total, boolean done) {
                                            MyLog.e(TAG, Looper.myLooper()+"");
                                            MyLog.e(TAG, "onProgress: " + "total ---->" + total + "done ---->" + progress );
                                        }
                                    }))
                                    .build();
                        }
                    })
                    .build();
             mApi = builder.client(client)
                    .build().create(HttpInterf.class);
        }
        return mApi;
    }

    public interface HttpInterf {

        @Streaming
        @GET
        Call<ResponseBody> downloadFile(@Url String fileUrl);
    }



    public interface ProgressListener {
        /**
         * @param progress     已经下载或上传字节数
         * @param total        总字节数
         * @param done         是否完成
         */
        void onProgress(long progress, long total, boolean done);
    }

    public static class ProgressResponseBody extends ResponseBody {

        private final ResponseBody responseBody;
        private final ProgressListener listener;
        private BufferedSource bufferedSource;

        public ProgressResponseBody(ResponseBody responseBody,ProgressListener listener){
            this.responseBody = responseBody;
            this.listener = listener;
        }
        @Override
        public MediaType contentType() {
            return responseBody.contentType();
        }

        @Override
        public long contentLength() {
            return responseBody.contentLength();
        }

        @Override
        public BufferedSource source() {
            if (null == bufferedSource){
                bufferedSource = Okio.buffer(source(responseBody.source()));
            }
            return bufferedSource;
        }

        private Source source(Source source) {
            return new ForwardingSource(source) {
                long totalBytesRead = 0L;

                @Override
                public long read(Buffer sink, long byteCount) throws IOException {
                    long bytesRead = super.read(sink, byteCount);
                    totalBytesRead += bytesRead != -1 ? bytesRead : 0;
                    listener.onProgress(totalBytesRead, responseBody.contentLength(), bytesRead == -1);
                    return bytesRead;
                }
            };
        }
    }
}
